import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,Input } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import Hearder from './hearder'
import '../card/card.scss'
import cat01 from '../../assets/kedouimg01/cat01.png'

export default class Index extends Component {
  constructor(props) {
    super(props)
    this.state = {
    	isload:1,
    	list:'',
    	type:''
    }
  }
  config = {
    navigationBarTitleText:'我的银行卡'
  }
  open(url,type){
    CommonJs.open(url)
  }
  componentWillMount () {
  	var type=this.$router.params.type
  	this.setState({
      type:type
    });
    this.getdate();
    
  }
  componentDidShow () {

  }
  del(index){
  	var _this=this;
  	if(this.state.type){
  		var arr=['删除','选取']
  	}
  	else{
  		var arr=['删除']
  	}
  	Taro.showActionSheet({
		  itemList:arr,
		  success: function (res) {
		    var tpindex=res.tapIndex;
		    if(tpindex==0){
		    	var url='/pay/papi/v1.9/bank/'+_this.state.list[index].id
			  	CommonJs.ajaxRequest(url,'','DELETE',function(res){
				     Taro.showToast({
								title:'删除成功',
								icon: 'none',
								duration: 2000
							});
							setTimeout(function(){
				      	_this.getdate();
				      },1000)
				  },'wfw')
		    }
		    else if(tpindex==1){
		    	let pages = getCurrentPages();
					let prevPage = pages[pages.length-2];
					prevPage.setData({
					  data:_this.state.list[index]
					});
					Taro.navigateBack({
					  delta: 1
					})
		    }
		  },
		  fail: function (res) {
		    console.log(res.errMsg)
		  }
		})
  	
  }
  getdate(){
  	var url='/pay/papi/v1.9/bank';
  	var _this=this;
  	CommonJs.ajaxRequest(url,'','GET',function(res){
	     _this.setState({
	      list:res.data.list,
	      isload:1
	    })
	  },'wfw')
  }
  
  render () {
    const list = this.state.list
    return (
      <View className='page' style={this.state.isload==1?'display:block':'display:none'}>
      	<View className='card-page' style={list.length>0?'display:block':'display:none'}>
      			{list.map((item, index) => (
	      			<View className='card-list' onClick={this.del.bind(this,index)}>
	      					<View className='card-dl'>
	      						
	      						<View  className='card-dd' style="margin-left:0">
	      							<Text className='card-h3'>{item.bankType.name}</Text>
	      							<Text className='card-price'>{item.account}</Text>
	      						</View>
	      					</View>
	      					<View className='card-info'>
	      						<Text className='card-fr'>{item.realname}</Text>
	      						<Text className='card-qun'>{item.branch}</Text>
	      					</View>
	      			</View>
	      		))}
      	</View>
      	<View className='card-page' style={list.length>0?'display:none':'display:block'}>
      		<View className='nodate'>暂无数据</View>
      	</View>
      	<View className='btn01' style="width:94%" onClick={this.open.bind(this,'/pages/my/bankadd?type='+this.state.type)}>添加银行卡</View>
      </View>
    )
  }
}
